package com.odyssey.enemies;

import org.anddev.andengine.engine.camera.BoundCamera;
import org.anddev.andengine.entity.layer.tiled.tmx.TMXTile;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.opengl.texture.region.TiledTextureRegion;

import com.odyssey.common.Definitions;
import com.odyssey.common.Direction;


public class Boss extends Enemy
{
  private boolean isActive;
  private boolean isJumping;
  private int baseHeight;
  
  //constructor ***********************************************************
  public Boss(int x, int y, BoundCamera cam, TiledTextureRegion texture, Scene inScene, int hp)
  {
    super(x, y, cam, texture, inScene, hp);
    isActive = false;
    isJumping = false;
    this.setBoss(true);
    baseHeight = 700;
    eSprite.setScale(2f);
    eCol.r = eCol.r*(3/2);
  }
  
  
  //miscellaneous****************************************************
  
  
  //getters & setters************************************************
  
  
  //collision & falling stuff****************************************
  
  private boolean falling(TMXTile below)
  {
    //check if there is block underneath, otherwise gravity fall

    if (below == null) 
    {
      return false;
    }
    else if (below.getGlobalTileID() == 124)
    {
      return false;
    }
    else 
    {
      //Log.v("EECS285 PROJECT4", "belowTileId: " +below.getGlobalTileID());
      return true;
    }
  }
  
  
  //artifical intelligence stuff*************************************
  
  
  //update loop******************************************************
  public void updateAnimation()
  {
    if (oldFace != facing)
    {
      if (facing == Direction.RIGHT)
        eSprite.animate(new long[]{200, 200, 200, 200, 200}, 5, 9, true);
      else if (facing == Direction.LEFT)
        eSprite.animate(new long[]{200, 200, 200, 200, 200}, 0, 4, true);
      oldFace = facing;
    } 
  }
  
  
  @Override
  public void enemyUpdate(TMXTile left, TMXTile right, TMXTile above, TMXTile below, TMXTile fall)
  {
    int rL = (int) (eCam.getCenterX() + eCam.getWidth()/2);
    if (rL + 100 >= eLoc.x) isActive = true;
    
    if (!isActive) return;
    /*
    if (jump == false && Math.abs(mLoc.x - jumpland) >= 50)
    {
      jump = true;
      jumping = true;
    }
    */
    
    if (isJumping == false)
    {
      if (falling(below))
        eLoc.y += Definitions.bvelocity;
    }
    
    if (isJumping == true)
      eLoc.y -= Definitions.bvelocity;
    
    if ((eLoc.y - baseHeight) <= 50) {
      isJumping = false;
    }
    
    if ((eLoc.y - baseHeight) > 130) {
      isJumping = true;
    }
    
    if(facing == Direction.LEFT)
    {
      if (collision(left,right)) facing = Direction.RIGHT;
      else 
      {
        eLoc.x -= Definitions.bvelocity;
        eCol.x -= Definitions.bvelocity;
      }
      //move left
    }
    
    if(facing == Direction.RIGHT)
    {
      if (collision(left,right)) facing = Direction.LEFT;
      else 
      {
        eLoc.x += Definitions.bvelocity;
        eCol.x += Definitions.bvelocity;
      }
      //move right
    }
    
    hpBar.setPosition(eLoc.x - 5, eLoc.y - 5, eLoc.x - 5 + Definitions.hpbarsize , eLoc.y - 5);
    eSprite.setPosition(eLoc.x, eLoc.y);
  }
  

}